package com.example.administrator.coldlain.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.administrator.coldlain.Api.ApiService;
import com.example.administrator.coldlain.R;
import com.example.administrator.coldlain.adapter.FragmentAdapter;
import com.example.administrator.coldlain.utils.SPUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener, ViewPager.OnPageChangeListener {
    private ListView listview;
    String allbaseurl;
    List<HashMap<String, Object>> menulist = new ArrayList<>();
    RadioGroup radioGroup;

    private Button category;
    private Button settingbtn;
    private DrawerLayout homelayout;
    private LinearLayout sublayout;
    private ViewPager viewPager;
    private FragmentAdapter fragementAdapter;
    private RadioButton radio_chats;
    private RadioButton radio_contact;
    private RadioButton radio_me;

    SharedPreferences sharedPreferences;
    private ViewPager layer_main;
    private RadioGroup radioGroup_main;
    private TextView username;


    EditText iptext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initMenuView();
        initFragments();
        inituserinfo();
    }

    private boolean inituserinfo() {
        boolean flag = false;
        sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
        String usernamestr = sharedPreferences.getString("username", "");
        if (usernamestr.equals(""))
            username.setText("用户未登录");
        else {
            username.setText("当前用户为" + usernamestr);
            flag = true;
        }
        return flag;
    }


    private void initView() {
        category = (Button) findViewById(R.id.category);
        settingbtn = (Button) findViewById(R.id.settingbtn);
        category.setOnClickListener(this);
        settingbtn.setOnClickListener(this);
        homelayout = (DrawerLayout) findViewById(R.id.homelayout);
        sublayout = (LinearLayout) findViewById(R.id.sublayout);
        radio_chats = (RadioButton) findViewById(R.id.radio_chats);
        radio_contact = (RadioButton) findViewById(R.id.radio_contact);
        radio_me = (RadioButton) findViewById(R.id.radio_me);
        layer_main = (ViewPager) findViewById(R.id.layer_main);
        layer_main.setOnClickListener(this);
        radioGroup_main = (RadioGroup) findViewById(R.id.radioGroup_main);
        radioGroup_main.setOnClickListener(this);
        username = (TextView) findViewById(R.id.username);
        username.setOnClickListener(this);
    }

    private void initMenuView() {
        HashMap<String, Object> hashMap;
        hashMap = new HashMap<>();
        hashMap.put("img", R.drawable.one);
        hashMap.put("title", "用户管理");
        menulist.add(hashMap);
        hashMap = new HashMap<>();
        hashMap.put("img", R.drawable.two);
        hashMap.put("title", "车间管理");
        menulist.add(hashMap);
        hashMap = new HashMap<>();
        hashMap.put("img", R.drawable.three);
        hashMap.put("title", "设备管理");
        menulist.add(hashMap);
        hashMap = new HashMap<>();
        hashMap.put("img", R.drawable.four);
        hashMap.put("title", "用户退出");
        menulist.add(hashMap);
        listview = findViewById(R.id.menulist);
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, menulist,
                R.layout.menu_item,
                new String[]{"img", "title"},
                new int[]{R.id.home_item_img, R.id.home_item_name});
        listview.setAdapter(simpleAdapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 3:
                        userexit();
                        break;
                }
            }
        });
    }

    private void userexit() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.logo)
                .setTitle("用户退出")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
                        SharedPreferences.Editor editor=sharedPreferences.edit();
                        editor.putString("username","");
                        editor.putString("password","");
                        editor.commit();
                        username.setText("用户未登录");
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        return;
                    }
                }).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.category:
                homelayout.openDrawer(sublayout);
                break;
            case R.id.settingbtn:
                //startActivity(new Intent(MainActivity.this,SetIPActivity.class));
                setIp();

                break;
        }
    }
    EditText set_ip_et;
    EditText set_ip_port_et;
    private void setIp() {
        LayoutInflater layoutInflater=getLayoutInflater();
        View view=layoutInflater.inflate(R.layout.setip,null);
        set_ip_et = view.findViewById(R.id.et_set_ip);
        set_ip_port_et = view.findViewById(R.id.et_set_ip_port);
        set_ip_et.setText(SPUtil.getInstance(MainActivity.this).getSettingParam("set_ip", ApiService.SET_IP));
        set_ip_port_et.setText(SPUtil.getInstance(MainActivity.this).getSettingParam("set_ip_port", ApiService.SET_IP_POST));

        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (set_ip_port_et.getText().toString().isEmpty() || set_ip_et.getText().toString().isEmpty()) {
                            showToast(MainActivity.this, "请输入IP和端口号");
                        } else {
                            allbaseurl = "http://" + set_ip_et.getText().toString() + ":" + set_ip_port_et.getText().toString();
                            SPUtil.getInstance(MainActivity.this).setSettingParam("set_ip", set_ip_et.getText().toString());
                            SPUtil.getInstance(MainActivity.this).setSettingParam("set_ip_port", set_ip_port_et.getText().toString());
                            SPUtil.getInstance(MainActivity.this).setSettingParam("baseurl", allbaseurl);
                            showToast(MainActivity.this, "设置成功！");
                            return;

                        }
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        return;
                    }
                }).show();
    }
    private static Toast toast;
    public static void showToast(Activity context, String content){
        if(toast==null){
            toast=Toast.makeText(context.getApplicationContext(),content,Toast.LENGTH_SHORT);
        }else{
            toast.setText(content);
        }
        toast.show();
    }
    void initFragments() {
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup_main);
        radioGroup.setOnCheckedChangeListener(this);
        radioGroup.check(R.id.radio_chats);
        fragementAdapter = new FragmentAdapter(getSupportFragmentManager());
        viewPager = (ViewPager) findViewById(R.id.layer_main);
        viewPager.setAdapter(fragementAdapter);
        viewPager.setCurrentItem(0);
        viewPager.setOnPageChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.radio_chats:
                viewPager.setCurrentItem(0);
                break;
            case R.id.radio_contact:
                viewPager.setCurrentItem(1);
                break;
            case R.id.radio_me:
                viewPager.setCurrentItem(2);
                break;
            default:
                break;
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //state的状态有三个，0表示什么都没做，1正在滑动，2滑动完成
        if (state == 2) {
            //对应值需要与 FragmentPagerAdapter 中的对应
            switch (viewPager.getCurrentItem()) {
                case 0:
                    radio_chats.setChecked(true);
                    break;
                case 1:
                    radio_contact.setChecked(true);
                    break;
                case 2:
                    radio_me.setChecked(true);
                    break;
            }
        }

    }
}
